using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.PatientDomain;
using ShivamEMR.Model.DataInterfaces.PharmacyDataInterf;
using ShivamEMR.Model.DataInterfaces.PatientDataInterf;
using ShivamEMR.Model.DataInterfaces.DoctorDataInterf;

namespace ShivamEMR.Web.Areas.Patient.Controllers
{   
    public class MedicationController : Controller
    {
		private readonly IDrugRepository drugRepository;
		private readonly IMedicationStatusRepository medicationstatusRepository;
		private readonly IPatientRepository patientRepository;
		private readonly IDoctorRepository doctorRepository;
		private readonly IDiagnosisRepository diagnosisRepository;
		private readonly IMedicationRepository medicationRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public MedicationController()// : this(new DrugRepository(), new MedicationStatusRepository(), new PatientRepository(), new DoctorRepository(), new DiagnosisRepository(), new MedicationRepository())
        {
        }

        public MedicationController(IDrugRepository drugRepository, IMedicationStatusRepository medicationstatusRepository, IPatientRepository patientRepository, IDoctorRepository doctorRepository, IDiagnosisRepository diagnosisRepository, IMedicationRepository medicationRepository)
        {
			this.drugRepository = drugRepository;
			this.medicationstatusRepository = medicationstatusRepository;
			this.patientRepository = patientRepository;
			this.doctorRepository = doctorRepository;
			this.diagnosisRepository = diagnosisRepository;
			this.medicationRepository = medicationRepository;
        }

        //
        // GET: /Medication/

        public ViewResult Index()
        {
            return View(medicationRepository.All);//Including(medication => medication.Drug, medication => medication.Patient, medication => medication.Diagnosis));
        }

        //
        // GET: /Medication/Details/5

        public ViewResult Details(System.Guid id)
        {
            return View(medicationRepository.Find(id));
        }

        //
        // GET: /Medication/Create

        public ActionResult Create()
        {
			ViewBag.PossibleDrugs = drugRepository.All;
			ViewBag.PossibleMedicationStatus = medicationstatusRepository.All;
			ViewBag.PossiblePatients = patientRepository.All;
			ViewBag.PossibleDoctors = doctorRepository.All;
			ViewBag.PossibleDiagnosis = diagnosisRepository.All;
            return View();
        } 

        //
        // POST: /Medication/Create

        [HttpPost]
        public ActionResult Create(Medication medication)
        {
            if (ModelState.IsValid) {
                medicationRepository.Create(medication);
                medicationRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleDrugs = drugRepository.All;
				ViewBag.PossibleMedicationStatus = medicationstatusRepository.All;
				ViewBag.PossiblePatients = patientRepository.All;
				ViewBag.PossibleDoctors = doctorRepository.All;
				ViewBag.PossibleDiagnosis = diagnosisRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Medication/Edit/5
 
        public ActionResult Edit(System.Guid id)
        {
			ViewBag.PossibleDrugs = drugRepository.All;
			ViewBag.PossibleMedicationStatus = medicationstatusRepository.All;
			ViewBag.PossiblePatients = patientRepository.All;
			ViewBag.PossibleDoctors = doctorRepository.All;
			ViewBag.PossibleDiagnosis = diagnosisRepository.All;
             return View(medicationRepository.Find(id));
        }

        //
        // POST: /Medication/Edit/5

        [HttpPost]
        public ActionResult Edit(Medication medication)
        {
            if (ModelState.IsValid) {
                medicationRepository.Update(medication);
                medicationRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleDrugs = drugRepository.All;
				ViewBag.PossibleMedicationStatus = medicationstatusRepository.All;
				ViewBag.PossiblePatients = patientRepository.All;
				ViewBag.PossibleDoctors = doctorRepository.All;
				ViewBag.PossibleDiagnosis = diagnosisRepository.All;
				return View();
			}
        }

        //
        // GET: /Medication/Delete/5
 
        public ActionResult Delete(System.Guid id)
        {
            return View(medicationRepository.Find(id));
        }

        //
        // POST: /Medication/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(System.Guid id)
        {
            var m = medicationRepository.Find(id);
            medicationRepository.Delete(m);
            medicationRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

